package com.prs.crm.action.crm;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.crm.CodeNumber;
import com.prs.crm.domain.crm.Configure;
import com.prs.crm.domain.crm.CustomerProject;
import com.prs.crm.domain.crm.ProjectContract;
import com.prs.crm.service.crm.CodeNumberService;
import com.prs.crm.service.crm.ConfigureService;
import com.prs.crm.service.crm.CustomerProjectService;
import com.prs.crm.service.crm.ProjectContractService;

public class ProjectContractAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private ProjectContract projectContract;
	private CustomerProject customerProject;
	private Integer[] projectIds;
	private Integer[] contractIds;
	private PaginationSupport pagination;
	private CodeNumber codeNumber;
	
	private CodeNumberService codeNumberService;
	private CustomerProjectService customerProjectService;
	private ProjectContractService projectContractService;
	private List<Configure> configures;
	private ConfigureService configureService;
	
	@Action("listProjectContract")
	public String listProjectContract(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ProjectContract.class);
		if(projectIds!=null){
			customerProject=this.customerProjectService.get(projectIds[0]);
			criteria.add(Restrictions.eq("customerProject", customerProject));
		}
		if(customerProject!=null){
		if(customerProject.getId()!=null){
			criteria.add(Restrictions.eq("customerProject", customerProject));
		}
		}
		criteria.add(Restrictions.or(Restrictions.eq("recorder.id", AppContext.getAccountContext().currentUser().getId()), Restrictions.like("person","%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%")));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.projectContractService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editProjectContract")
	public String editProjectContract(){
		if(this.contractIds!=null){
			this.projectContract=this.projectContractService.get(contractIds[0]);
		}else{
			this.codeNumber=this.codeNumberService.getCodeByState("合同");
		}
		this.configures=this.configureService.getConfigureByProContract(projectContract);
		return SUCCESS;
	}
	@Action("viewProjectContract")
	public String viewProjectContract(){
		if(contractIds!=null){
			this.projectContract=this.projectContractService.get(contractIds[0]);
			this.customerProject=this.projectContract.getCustomerProject();
		}else{
			this.projectContract=(ProjectContract) this.projectContractService.getContractByProject(customerProject);
			
		}
		this.configures=this.configureService.getConfigureByProContract(projectContract);
		return SUCCESS;
	}
	@Action(value = "removeProjectContract", results = {
			@Result(name = "success", type = "redirect", location = "listProjectContract"),
			@Result(name = "error", type = "chain", location = "listProjectContract") })
	public String removeProjectContract(){
		this.projectContractService.batchRemove(contractIds);
		return SUCCESS;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public ProjectContract getProjectContract() {
		return projectContract;
	}

	public void setProjectContract(ProjectContract projectContract) {
		this.projectContract = projectContract;
	}

	public ProjectContractService getProjectContractService() {
		return projectContractService;
	}

	public void setProjectContractService(
			ProjectContractService projectContractService) {
		this.projectContractService = projectContractService;
	}

	public Integer[] getProjectIds() {
		return projectIds;
	}

	public void setProjectIds(Integer[] projectIds) {
		this.projectIds = projectIds;
	}

	public CustomerProject getCustomerProject() {
		return customerProject;
	}

	public void setCustomerProject(CustomerProject customerProject) {
		this.customerProject = customerProject;
	}

	public CustomerProjectService getCustomerProjectService() {
		return customerProjectService;
	}

	public void setCustomerProjectService(
			CustomerProjectService customerProjectService) {
		this.customerProjectService = customerProjectService;
	}
	public Integer[] getContractIds() {
		return contractIds;
	}
	public void setContractIds(Integer[] contractIds) {
		this.contractIds = contractIds;
	}
	public List<Configure> getConfigures() {
		return configures;
	}
	public void setConfigures(List<Configure> configures) {
		this.configures = configures;
	}
	public ConfigureService getConfigureService() {
		return configureService;
	}
	public void setConfigureService(ConfigureService configureService) {
		this.configureService = configureService;
	}
	public CodeNumber getCodeNumber() {
		return codeNumber;
	}
	public void setCodeNumber(CodeNumber codeNumber) {
		this.codeNumber = codeNumber;
	}
	public CodeNumberService getCodeNumberService() {
		return codeNumberService;
	}
	public void setCodeNumberService(CodeNumberService codeNumberService) {
		this.codeNumberService = codeNumberService;
	}
	
	
}
